package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumInfoDetailVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient ;

    @Autowired
    private CategoryFeignClient categoryFeignClient ;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient ;
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public AlbumInfoDetailVo getItem(Long albumId) {

        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        if (!bloomFilter.contains(albumId)){
            log.error("bloomFilter判断专辑id：{}，不存在",albumId);
            return null;
        }
        // 创建AlbumInfoDetailVo对象，封装响应结果数据
        AlbumInfoDetailVo albumInfoDetailVo = new AlbumInfoDetailVo();

        // 远程调用：根据专辑的id查询专辑基本数据
        CompletableFuture<AlbumInfo> cf1 = CompletableFuture.supplyAsync(() -> {
            Result<AlbumInfo> albumInfo = albumInfoFeignClient.getAlbumInfo(albumId);
            AlbumInfo albumInfoData = albumInfo.getData();
            albumInfoDetailVo.setAlbumInfo(albumInfoData);
            return albumInfoData;
        });


        // 远程调用：根据专辑的id查询专辑统计数据
        CompletableFuture<Void> cf2 = CompletableFuture.runAsync(() -> {
            Result<AlbumStatVo> albumStatVo = albumInfoFeignClient.getAlbumStatVo(albumId);
            AlbumStatVo albumStatVoData = albumStatVo.getData();
            albumInfoDetailVo.setAlbumStatVo(albumStatVoData);
        });


        // 远程调用：根据三级分类的id查询分类数据
        CompletableFuture<Void> cf3 = cf1.thenAcceptAsync(albumInfo -> {
            Result<BaseCategoryView> categoryBy3Id = categoryFeignClient.findByCategoryBy3Id(albumInfo.getCategory3Id());
            BaseCategoryView categoryBy3IdData = categoryBy3Id.getData();
            albumInfoDetailVo.setBaseCategoryView(categoryBy3IdData);
        });

        // 远程调用：根据用户的id查询作者数据
        CompletableFuture<Void> cf4 = cf1.thenAcceptAsync(albumInfo -> {
            Result<UserInfo> userInfoById = userInfoFeignClient.findUserInfoById(albumInfo.getUserId());
            UserInfo userInfoByIdData = userInfoById.getData();
            UserInfoVo userInfoVo = new UserInfoVo();
            BeanUtils.copyProperties(userInfoByIdData, userInfoVo);
            albumInfoDetailVo.setAnnouncer(userInfoVo);
        });

        CompletableFuture.allOf(cf1, cf2, cf3, cf4).join();

        return albumInfoDetailVo;
    }
}
